package com.spc.boot.common.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    public static String join(Object[] arr, String split) {
        StringBuilder sb = new StringBuilder(arr.length * 3);
        int offset = arr.length - 1;
        for (int i = 0; i < offset; i++) {
            sb.append(arr[i]).append(split);
        }
        sb.append(arr[offset]);

        return sb.toString();
    }

    public static final String PRICEFORMATE = "########0.00";

    /**
     * 将字符串转换成整数
     * 
     * @param in
     *            String : 要转换的字符串"a"
     * @param defaultValue
     *            int : 默认值0
     * @return int : 如果字符串合法（可以转换）则返回转换后的整数，否则返回默认值
     */
    public static int parseInt(final Object in, final int defaultValue) {
        if (in != null) {
            return parseInt(in.toString(), defaultValue);
        }
        return defaultValue;
    }

    /**
     * 将字符串转换成整数
     * 
     * @param in
     *            String : 要转换的字符串"a"
     * @param defaultValue
     *            int : 默认值0
     * @return int : 如果字符串合法（可以转换）则返回转换后的整数，否则返回默认值
     */
    public static int parseInt(final String in, final int defaultValue) {
        if (in == null)
            return defaultValue;
        try {
            return Integer.parseInt(in);
        } catch (NumberFormatException nfe) {
            if (in.equalsIgnoreCase("true")) {
                return 1;
            }
            if (in.equalsIgnoreCase("false")) {
                return 0;
            }
            return defaultValue;
        }
    }

    public static Integer parseInteger(final String in, final int defaultValue) {
        return new Integer(parseInt(in, defaultValue));
    }

    /**
     * 判断是否为正确的邮箱地址
     * 
     * @param in
     *            String : 邮箱地址
     * @return boolean : 如果是合法邮箱地址返回true，否则返回false
     */
    public static boolean isEmail(final String in) {
        if (StringUtil.isEmpty(in))
            return false;
        int at = in.indexOf("@");
        int dot = in.indexOf(".");
        if (at < 0 || dot < 0)
            return false;
        if ((at + 1) >= dot)
            return false;
        if ((dot + 1) >= in.length())
            return false;
        return true;
    }

    /**
     * 将字符串转换成double
     * 
     * @param in
     *            String : 要转换的字符串
     * @param defaultValue
     *            double : 默认值
     * @return double : 如果字符串可以转换，则返回转换后的double，否则返回默认值
     */
    public static double parseDouble(final Object in, final double defaultValue) {
        if (in != null) {
            return parseDouble(in.toString(), defaultValue);
        }
        return defaultValue;
    }

    /**
     * 将字符串转换成double
     * 
     * @param in
     *            String : 要转换的字符串
     * @param defaultValue
     *            double : 默认值
     * @return double : 如果字符串可以转换，则返回转换后的double，否则返回默认值
     */
    public static double parseDouble(final String in, final double defaultValue) {
        if (in == null)
            return defaultValue;
        try {
            return Double.parseDouble(in);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * 判断字符串是否为空或是空串
     * 
     * @param in
     *            String : 待判断的字符串
     * @return boolean : 为空或为空串均返回true，否则返回false
     */
    public static boolean isEmpty(final String in) {
        if (in == null || in.trim().length() <= 0)
            return true;
        return false;
    }

    /**
     * 判断字符串是否为非空或是非空串
     *
     * @param in
     *            String : 待判断的字符串
     * @return boolean : 为空或为空串均返回true，否则返回false
     */
    public static boolean isNoEmpty(final String in) {
        return !isEmpty(in);
    }

    /**
     * 取得字符串指定编码的字符显示
     * 
     * @param in
     *            String : 源字符串
     * @param charset
     *            String : 编码
     * @return String : 返回相应的字符串，如果指定的编码不合法，则返回源字符串
     */
    public static String changeEncoding(final String in, final String charset) {
        if (in == null)
            return null;
        try {
            return new String(in.getBytes(charset));
        } catch (UnsupportedEncodingException uee) {
            return in;
        }
    }

    /**
     * 将字符串转换成long
     * 
     * @param in
     *            String : 要转换的字符串
     * @param defaultValue
     *            long : 默认值
     * @return long : 如果字符串可以转换，则返回转换后的long，否则返回默认值
     */
    public static long parseLong(final Object in, final long defaultValue) {
        if (in != null) {
            return parseLong(in.toString(), defaultValue);
        }
        return defaultValue;
    }

    /**
     * 将字符串转换成long
     * 
     * @param in
     *            String : 要转换的字符串
     * @param defaultValue
     *            long : 默认值
     * @return long : 如果字符串可以转换，则返回转换后的long，否则返回默认值
     */
    public static long parseLong(final String in, final long defaultValue) {
        if (in == null || in.trim().length() == 0)
            return defaultValue;
        try {
            return Long.parseLong(in);
        } catch (NumberFormatException nfe) {
            if (in.equalsIgnoreCase("true")) {
                return 1;
            }
            if (in.equalsIgnoreCase("false")) {
                return 0;
            }
            return defaultValue;
        }
    }

    /**
     * 字符串是否合法
     * 
     * @param in
     *            String : 待判断的字符串
     * @param legal
     *            String : 所有的合法字符
     * @return boolean : 如果字符串里不合任何非法字符，则返回ture,否则返回false
     */
    public static boolean isLegal(final String in, final String legal) {
        if (legal == null || legal.length() <= 0)
            return false;
        if (in == null || in.length() <= 0)
            return true;
        for (int i = 0; i < in.length(); i++) {
            char c = in.charAt(i);
            if (legal.indexOf(c) < 0)
                return false;
        }
        return true;
    }

    public static final String ALL_LOWER_LETTER = "abcdefghijklmnopqrstuvwxyz"; // 小写字母

    public static final String ALL_UPPER_LETTER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 大写字母

    public static final String ALL_NUMERIC_LETTER = "0123456789"; // 数字

    /**
     * 判断字符串是否为正确的手机号码串，正确的手机号码是以"13"或"15"开头的11位数字字符串
     * 
     * @param in
     *            String : 待验证的号码串
     * @return boolean : 如果是，返回true，否则返回false
     */
    public static boolean isMobile(final String in) {
        if (in == null || in.length() < 0)
            return false;
        if (in.length() != 11)
            return false;
        if (isLegal(in, ALL_NUMERIC_LETTER) == false)
            return false;
        if (in.startsWith("13") || in.startsWith("15") || in.startsWith("18"))
            return true;
        return false;
    }

    /**
     * 判断是否为全数字字符串 654564654 = true 3434343.23 = true(xxxxx.xxxx格式为true) 323232.323.5213 = false wewe.1212 = false;
     * 
     * @param in
     *            String : 字符串
     * @return boolean : 如果全由数字组成返回true，否则返回false
     */
    public static boolean isNumberic(final String in) {
        if (in == null || in.length() <= 0)
            return false;
        boolean isNum = false;

        String num[] = in.split("\\.", 2);
        for (int i = 0; i < num.length; i++) {
            isNum = isLegal(num[i], ALL_NUMERIC_LETTER);
        }
        return isNum;
    }

    /**
     * 小写第一个字母
     * 
     * @param in
     *            String : 待格式化的字符串
     * @return String : 如果in==null或in长度为0则返回in
     */
    public static String lowerFirstChar(final String in) {
        if (in == null)
            return null;
        return in.substring(0, 1).toLowerCase() + in.substring(1);
    }

    /**
     * 大写第一个字母
     * 
     * @param in
     *            String : 待格式化的字符串
     * @return String : 如果in==null或in长度为0则返回in
     */
    public static String upperFirstChar(final String in) {
        if (in == null || in.length() <= 0)
            return in;
        return in.substring(0, 1).toUpperCase() + in.substring(1);

    }

    /**
     * 去掉指定字符串的开头和结尾的指定字符
     * 
     * @param stream
     *            要处理的字符串
     * @param trimstr
     *            要去掉的字符串
     * @return 处理后的字符串
     */
    public static String trim(String stream, String trimstr) {
        // null或者空字符串的时候不处理
        if (stream == null || stream.length() == 0 || trimstr == null || trimstr.length() == 0) {
            return stream;
        }

        // 结束位置
        int epos = 0;

        // 正规表达式
        String regpattern = "[" + trimstr + "]*+";
        Pattern pattern = Pattern.compile(regpattern, Pattern.CASE_INSENSITIVE);

        // 去掉结尾的指定字符
        StringBuffer buffer = new StringBuffer(stream).reverse();
        Matcher matcher = pattern.matcher(buffer);
        if (matcher.lookingAt()) {
            epos = matcher.end();
            stream = new StringBuffer(buffer.substring(epos)).reverse().toString();
        }

        // 去掉开头的指定字符
        matcher = pattern.matcher(stream);
        if (matcher.lookingAt()) {
            epos = matcher.end();
            stream = stream.substring(epos);
        }

        // 返回处理后的字符串
        return stream;
    }

    /**
     * 格式化输出数字
     * 
     * @param in
     *            double : 待输出数值
     * @param format
     *            String : 输出格式，如：#0.00,###,###.00
     * @return String : 格式化后的字符串
     */
    public static String toString(final Double in, final String format) {
        if (in == null) {
            return "0.00";
        }
        DecimalFormat formatter = new DecimalFormat(format);
        return formatter.format(in);
    }

    /**
     * 格式化输出数字
     * 
     * @param in
     *            double : 待输出数值
     * @param format
     *            String : 输出格式，如：#0.00,###,###.00
     * @return String : 格式化后的字符串
     */
    public static String toString(final BigDecimal in, final String format) {
        DecimalFormat formatter = new DecimalFormat(format);
        return formatter.format(in);
    }

    /**
     * 格式化输入字符串，对null值作处理
     * 
     * @param in
     *            String : 待输入的字符串
     * @param defaultNullValue
     *            String : 如果in == null时输出的默认值
     * @return String : 格式化后的字符串，如果in == null则返回默认值
     */
    public static String formatNullValue(Object in, Object defaultNullValue) {
        return (in == null || isEmpty(in.toString())) ? defaultNullValue.toString() : in.toString();
    }

    /**
     * 格式化输入字符串，对null值作处理
     * 
     * @param in
     *            String : 待输入的字符串
     * @param defaultNullValue
     *            String : 如果in == null时输出的默认值
     * @return String : 格式化后的字符串，如果in == null则返回默认值
     */
    public static String formatNullValue(Object in, String format, Object defaultNullValue) {
        return (in == null || isEmpty(in.toString())) ? defaultNullValue.toString() : String.format(format, in);
    }

    /**
     * 格式化输出数字
     * 
     * @param in
     *            int : 待输出的整数
     * @param format
     *            String : 输出格式，如：#0.00,###,###.00
     * @return String : 格式化后的字符串
     */
    public static String toString(final int in, final String format) {
        DecimalFormat formatter = new DecimalFormat(format);
        return formatter.format(in);
    }

    /**
     * 判断字符串是否包含有中文
     * 
     * @param str
     * @return boolean
     * @author ghliu
     */

    public boolean isIncludeChinessString(final String str) {
        if (str == null || "".equals(str)) {
            return false;
        }

        Pattern p = Pattern.compile("[\u4E00-\u9FA5]*");
        Matcher m = p.matcher(str);
        boolean result = m.matches();
        return result;
    }

    /**
     * 判断是否为身份证号码
     * 
     * @param str
     * @return boolean
     * @author ghliu
     */
    public static boolean isIDCard(String str) {
        if ("".equals(str) || str == null) {
            return false;
        }
        String[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"};
        String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
        String Ai = "";

        // ================ 号码的长度 15位或18位 ================
        if (str.length() != 15 && str.length() != 18) {
            return false;
        }
        // =======================(end)========================

        // ================ 数字 除最后以为都为数字 ================
        if (str.length() == 18) {
            Ai = str.substring(0, 17);
        } else if (str.length() == 15) {
            Ai = str.substring(0, 6) + "19" + str.substring(6, 15);
        }
        if (isNumberic(Ai) == false) {
            return false;
        }
        // =======================(end)========================

        // ================ 出生年月是否有效 ================
        String strMonth = Ai.substring(10, 12);// 月份
        String strDay = Ai.substring(12, 14);// 月份

        if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
            return false;
        }
        if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
            return false;
        }
        // =====================(end)=====================

        // ================ 地区码时候有效 ================
        Hashtable<String, String> h = getAreaCode();
        if (h.get(Ai.substring(0, 2)) == null) {
            return false;
        }
        // ==============================================

        // ================ 判断最后一位的值 ================
        int TotalmulAiWi = 0;
        for (int i = 0; i < 17; i++) {
            TotalmulAiWi = TotalmulAiWi + Integer.parseInt(String.valueOf(Ai.charAt(i))) * Integer.parseInt(Wi[i]);
        }
        int modValue = TotalmulAiWi % 11;
        String strVerifyCode = ValCodeArr[modValue];
        Ai = Ai + strVerifyCode;

        if (str.length() == 18) {
            if (Ai.equals(str) == false) {
                return false;
            }
        } else {
            return true;
        }
        // =====================(end)=====================
        return true;
    }

    private static Hashtable<String, String> getAreaCode() {
        Hashtable<String, String> hashtable = new Hashtable<String, String>();
        hashtable.put("11", "北京");
        hashtable.put("12", "天津");
        hashtable.put("13", "河北");
        hashtable.put("14", "山西");
        hashtable.put("15", "内蒙古");
        hashtable.put("21", "辽宁");
        hashtable.put("22", "吉林");
        hashtable.put("23", "黑龙江");
        hashtable.put("31", "上海");
        hashtable.put("32", "江苏");
        hashtable.put("33", "浙江");
        hashtable.put("34", "安徽");
        hashtable.put("35", "福建");
        hashtable.put("36", "江西");
        hashtable.put("37", "山东");
        hashtable.put("41", "河南");
        hashtable.put("42", "湖北");
        hashtable.put("43", "湖南");
        hashtable.put("44", "广东");
        hashtable.put("45", "广西");
        hashtable.put("46", "海南");
        hashtable.put("50", "重庆");
        hashtable.put("51", "四川");
        hashtable.put("52", "贵州");
        hashtable.put("53", "云南");
        hashtable.put("54", "西藏");
        hashtable.put("61", "陕西");
        hashtable.put("62", "甘肃");
        hashtable.put("63", "青海");
        hashtable.put("64", "宁夏");
        hashtable.put("65", "新疆");
        hashtable.put("71", "台湾");
        hashtable.put("81", "香港");
        hashtable.put("82", "澳门");
        hashtable.put("91", "国外");
        return hashtable;
    }

    /**
     * 
     * 正则表达式验证
     * 
     * @param regex
     *            正则表达式
     * @param str
     *            验证的字符串
     * @return true:验证通过 false:验证失败
     */
    public static boolean regex(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher ma = pattern.matcher(str);
        return ma.matches();
    }

    public final static String CHINESE_REGEX = "[\u4e00-\u9fa5]*"; // 匹配中文
    public final static String USERNAME_REGEX = "^[a-zA-Z][a-zA-Z0-9_]{5,30}$"; // 匹配帐户名
    public final static String TEL_REGEX = "\\d{3}-\\d{8}|\\d{4}-\\d{7}"; // 匹配国内电话
    public final static String POSTALCODE_REGEX = "[1-9]\\d{5}(?!\\d)"; // 匹配中国邮政编码
    public final static String TREUNAME_REGEX = "^[\u4e00-\u9fa5A-Za-z.]{1,60}"; // 匹配姓名
    public final static String UNILLEGAL_REGEX = "^[\u4e00-\u9fa5A-Za-z0-9.]*"; // 匹配非!@#$%^&*(){}[]_+=-=\|?/>.<,字符
    public final static String LETTERNUMBER_REGEX = "^[A-Za-z0-9]+$"; // 匹配英文数字字符
    public final static String NUMBER_REGEX = "^\\d+$"; // 匹配数字字符

    /**
     * 生成随机密码
     * 
     * @param num
     *            密码位数
     * @return String
     */
    public static String getRandomPassword(int num) {
        String def = "1234567890abcdefghijklmnopqrstuvwxyz";
        String pwd = "";
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            int p = random.nextInt(36);
            pwd += def.charAt(p);
        }
        return pwd;
    }

    /**
     * 数字不足length位左边补0
     * 
     * @author ghliu
     * @param i
     *            要补零的整数
     * @param length
     *            不足多少位补零
     * @return 返回左边补0后的字符串
     */
    public static String paddZeroLeft(String s, int length) {
        StringBuffer sb = new StringBuffer(s);
        while (sb.length() < length) {
            sb.insert(0, "0");
        }
        return sb.toString();
    }

    /**
     * 分拆字符串成列表
     * 
     * @param in
     *            String : 字符串
     * @param splitter
     *            String : 分隔符
     * @return List : 分拆后的字符列表
     */
    public static List<String> toSplit(final String in, final String splitter) {
        List<String> out = new LinkedList<String>();
        if (in == null || in.length() <= 0)
            return out;
        String str[] = in.split(splitter);
        for (int i = 0; i < str.length; i++) {
            if (!isEmpty(str[i]))
                out.add(str[i]);
        }
        return out;
    }

    /**
     * 将字符串首字母大写，其他字母小写
     * 
     * @param name
     * @return
     */
    public static String first2UOther2L(String name) {
        if (name == null || name.trim().length() == 0) {
            return null;
        }
        if (name.length() == 1) {
            return name.toUpperCase();
        }
        return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
    }

    /**
     * 将字符串首字母大写，其他字母大小写不变
     * 
     * @param name
     * @return
     */
    public static String first2U(String name) {
        if (name == null || name.trim().length() == 0) {
            return null;
        }
        if (name.length() == 1) {
            return name.toUpperCase();
        }
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    /**
     * 驼峰格式字符串 转换成 下划线格式字符串 eg: playerId -> player_id;<br>
     * playerName -> player_name;
     * 
     * @param param
     *            待转换字符串
     * @return
     */
    public static String camelCase2Underscore(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param != null && !param.equals("")) {
            StringBuilder builder = new StringBuilder(param);
            Matcher mc = p.matcher(param);

            for(int i = 0; mc.find(); ++i) {
                builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            }

            if ('_' == builder.charAt(0)) {
                builder.deleteCharAt(0);
            }

            return builder.toString();
        } else {
            return "";
        }
    }

    /**
     * 下划线格式字符串转换成驼峰格式字符串 eg: player_id -> playerId;<br>
     * player_name -> playerName;
     * 
     * @param strs
     *            待转化字符串
     * @return
     */
    public static String underScore2CamelCase(String strs) {
        String[] elems = strs.split("_");
        for (int i = 0; i < elems.length; i++) {
            elems[i] = elems[i].toLowerCase();
            if (i != 0) {
                String elem = elems[i];
                char first = elem.toCharArray()[0];
                elems[i] = "" + (char)(first - 32) + elem.substring(1);
            }
        }
        return Arrays.toString(elems);
    }

    /**
     * 精确数字
     * 
     * @param digital
     *            需要精确的数字
     * @param dig
     *            点位
     * @return
     */
    public static Double toDecimalDobule(Double digital, int dig) {
        if (digital == null) {
            return null;
        }

        DecimalFormat formatter = new DecimalFormat("#.00");
        formatter.setMaximumFractionDigits(dig);
        String str = formatter.format(digital);
        if (str != null) {
            return Double.parseDouble(str);
        }
        return 0.0;
    }

    public static String ToMoneyCH(Object number) {
        String[] MONEY_UNIT = {"拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分", "厘", "毫"};
        String[] DIGITAL_UNIT = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String retString = "";
        String num = number.toString();
        if (num.indexOf('-') > -1) {
            num = num.substring(1);
            retString += "负";
        }
        int intStart = 10;
        /// 将数字先分开成整数和小数部分
        if ("".equals(num.trim()))
            return "";
        try {
            float checkValue = Float.parseFloat(num);
            if (checkValue > 1000000000) {
                return "";
            }
        } catch (Exception e) {
            //// 文本不能转换成浮点小数，返回""
            return "";
        }
        String[] sValue = num.split("\\.");
        /// 整数部分长度
        int intLength = sValue[0].length();
        /// 前面的字符是否为
        boolean preIsZero = false;
        /// 处理整数
        for (char digital : sValue[0].toCharArray()) {
            if (!preIsZero) {
                if (digital == '0') {
                    preIsZero = true;

                    if ("亿万".indexOf(MONEY_UNIT[intStart - intLength]) >= 0)

                        retString += MONEY_UNIT[intStart - intLength];
                } else
                    retString += DIGITAL_UNIT[(int)digital - 48] + MONEY_UNIT[intStart - intLength];
            } else {
                if (digital != '0') {
                    retString += "零" + DIGITAL_UNIT[(int)digital - 48] + MONEY_UNIT[intStart - intLength];
                    preIsZero = false;
                } else {
                    if (intLength - 1 == 0)
                        retString += "元";
                    else if ("亿万".indexOf(MONEY_UNIT[intStart - intLength]) >= 0
                        && "亿万".indexOf(retString.substring(retString.length() - 1)) < 0)
                        retString += MONEY_UNIT[intStart - intLength];
                }
            }
            intLength--;
        }
        /// 处理小数
        if (sValue.length == 2 && sValue[1].length() > 0) {
            boolean AllIsZero = true;
            intLength = sValue[1].length();
            char[] items = sValue[1].toCharArray();
            for (int i = 0; i < intLength; i++) {
                char d = items[i];
                if (d != '0') {
                    AllIsZero = false;
                    retString += DIGITAL_UNIT[(int)d - 48] + MONEY_UNIT[10 + i];
                }
                if (i >= 3)
                    break;
            }
            if (AllIsZero)
                retString += "整";
        } else {
            retString += "整";
        }
        return retString == "整" ? "" : retString;
    }

    public static String toCH(int intInput) {
        String si = String.valueOf(intInput);
        String sd = "";
        if (si.length() == 1) { // 個
            sd += getCH(intInput);
            return sd;
        } else if (si.length() == 2) {// 十
            if (si.substring(0, 1).equals("1")) {
                sd += "十";
            } else {
                sd += (getCH(intInput / 10) + "十");
            }
            sd += toCH(intInput % 10);
        } else if (si.length() == 3) {// 百
            sd += (getCH(intInput / 100) + "百");
            if (String.valueOf(intInput % 100).length() < 2) {
                sd += "零";
            }
            sd += toCH(intInput % 100);
        } else if (si.length() == 4) {// 千
            sd += (getCH(intInput / 1000) + "千");
            if (String.valueOf(intInput % 1000).length() < 3) {
                sd += "零";
            }
            sd += toCH(intInput % 1000);
        } else if (si.length() == 5) {
            sd += (getCH(intInput / 10000) + "万");
            if (String.valueOf(intInput % 10000).length() < 4) {
                sd += "零";
            }
            sd += toCH(intInput % 10000);
        }

        return sd;
    }

    private static String getCH(int input) {
        String sd = "";
        switch (input) {
            case 1:
                sd = "一";
                break;
            case 2:
                sd = "二";
                break;
            case 3:
                sd = "三";
                break;
            case 4:
                sd = "四";
                break;
            case 5:
                sd = "五";
                break;
            case 6:
                sd = "六";
                break;
            case 7:
                sd = "七";
                break;
            case 8:
                sd = "八";
                break;
            case 9:
                sd = "九";
                break;
            default:
                break;
        }
        return sd;
    }

    public static String reverse(String stream) {
        return new StringBuffer(stream).reverse().toString();
    }

    /**
     * 拼接字符串
     * 
     * @param part
     * @return
     */
    public static String join(Object... part) {
        StringBuilder sb = new StringBuilder();
        for (Object o : part) {
            sb.append(o == null ? "" : o.toString());
        }
        return sb.toString();
    }

    public static String cut(String s, int length, Boolean showDot) {
        if (isEmpty(s) || s.length() <= length)
            return s;

        return s.substring(0, length) + (showDot ? "..." : "");
    }

    /**
     * 把对象转化成字符串
     * 
     * @param obj
     * @return
     */
    public static String objToString(Object obj) {
        String rs = "";
        if (obj != null) {
            rs = obj.toString();
        }
        return rs;
    }

    public static Object formatClob(Object value) {
        return formatClob(value, null);
    }

    public static Object formatClob(Object value, Object defaultValue) {
        try {
            if (value instanceof Clob) {
                Clob result = (Clob)value;
                if (result != null && result.length() > 0) {
                    return result.getSubString(1, (int)result.length());
                }
            } else if (value instanceof Blob) {
                Blob result = (Blob)value;
                if (result != null && result.length() > 0) {
                    return new String(result.getBytes((long)1, (int)result.length()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    public static String substringBeforeLast(final String str, final String separator) {
        if (isEmpty(str) || isEmpty(separator)) {
            return str;
        }
        final int pos = str.lastIndexOf(separator);
        if (pos == -1) {
            return str;
        }
        return str.substring(0, pos);
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 将Oracle字符转为 Java 规范。
     *
     * @param input
     * @return
     */
    public static String toHump(String input) {
        if (isEmpty(input) || !input.equals(input.toUpperCase())) {
            return input;
        }
        StringBuilder sb = new StringBuilder();
        char[] chars = input.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c != '_') {
                c = Character.toLowerCase(c);
                if (i > 0 && i < chars.length && chars[i - 1] == '_') {
                    c = Character.toUpperCase(c);
                }
                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static String toUpperline(String input) {
        if (isEmpty(input) || input.equals(input.toUpperCase())) {
            return input;
        } else {
            return input.replaceAll("([A-Z])", "_$1").toUpperCase();
        }
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    //生成以时间戳和随机数组成的随机数字
    public static String randomName(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());
        Integer random = new Random().nextInt(9999) % (9000) + 1000;
        return timestamp + random;
    }

    //判断一组字符串非空，并且排除前端传来的null和undefined
    //只要有一个为空，返回false，全部不为空false返回true
    public static boolean hasNotNull(final String ...strings) {
	    if (strings == null || strings.length < 1){
	        return false;
        }
        for (String str : strings) {
            if(isBlank(str) || "null".equalsIgnoreCase(str) || "undefined".equalsIgnoreCase(str)) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasNull(final String ...strings) {
	    return !hasNotNull(strings);
    }

    //字符串左包含，返回true代替左边匹配
    public static boolean hasLeft(final String source,final String str){
	    if(isBlank(source) || isBlank(str) || !source.contains(str)){
	        return false;
        }
	    return str.equals(source.substring(0, str.length()));
    }

    //同上，这次是右包含
    public static boolean hasRight(final String source,final String str){
        if(isBlank(source) || isBlank(str) || !source.contains(str)){
            return false;
        }
        int startIndex = source.length() - str.length();
        return str.equals(source.substring(startIndex));
    }

}
